﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class SpriteSequence
{
    [System.Serializable]
    public class Config
    {
        public Sprite sprite;
        public float intervalTime = 1f;
        public bool interactive = false;
        public GameObject part;
        public bool hiddenLastPart;
        public Vector3 position;
        public Quaternion rotation;
        public Vector3 scale;
        private bool done;
        public void SetDone(bool v)
        {
            done = v;
        }
        public bool IsDone()
        {
            return done;
        }
    }
    public Config[] configSequence;
    public bool Done
    {
        get
        {
            return !inProgress;
        }
    }
    private float startTime;
    private bool inProgress;

    private int currentIndex;

    public void SetStartTime(float v)
    {
        startTime = v;
    }
    public float GetStartTime()
    {
        return startTime;
    }
    public void Reset()
    {
        foreach (Config config in configSequence)
        {
            config.SetDone(false);
        }
        inProgress = false;
        startTime = 0;
    }
    public void Start()
    {
        startTime = Time.time;
    }
    public void Restart()
    {
        this.Reset();
        this.Start();
    }
    public bool Update(GameObject protagonist, Vector3 scale)
    {
        float dt = Time.time - startTime;
        Transform transform = null;
        if (protagonist != null)
        {
            transform = protagonist.transform;
        }
        //inProgress = false;
        for (int i = 0; i < configSequence.Length; i++)
        {
            currentIndex = i;
            GameObject lastPart = i > 0 ? configSequence[i - 1].part : null;
            Config config = configSequence[i];
            if (dt >= 0 && dt <= config.intervalTime)
            {
                if (!config.IsDone())
                {
                    if (config.hiddenLastPart && lastPart != null)
                    {
                        lastPart.SetActive(false);
                    }
                    if (config.part != null)
                    {
                        config.part.SetActive(true);
                        if (config.part.TryGetComponent<SpriteRenderer>(out var sr))
                        {
                            sr.sprite = config.sprite;
                        }
                        if (transform != null)
                        {
                            transform.position += new Vector3(
                                config.position.x * scale.x,
                                config.position.y * scale.y,
                                config.position.z * scale.z
                                );
                        }
                    }
                    config.SetDone(true);
                }
                //inProgress = true;
                break;
            }
            dt -= config.intervalTime;
        }
        inProgress = configSequence.FindWith((Config config) =>
        {
            return !config.IsDone();
        }) != null;
        return inProgress;
    }

    public int GetCurrentIndex()
    {
        return currentIndex;
    }

}
